package com.kanshu.ksgb.fastread.doudou.module.book.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wcy on 18/5/27.
 */

public class AdBookDetailsTwoLayout extends LinearLayout {
    @BindView(R.id.label)
    TextView mLabel;
    @BindView(R.id.change)
    TextView mChange;
    @BindView(R.id.container)
    LinearLayout mContainer;
    SparseArray<View> mSparseArray = new SparseArray<>();

    public AdBookDetailsTwoLayout(Context context) {
        super(context);
        init();
    }

    public AdBookDetailsTwoLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdBookDetailsTwoLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_book_details_type_two_layout, this);
        ButterKnife.bind(view);
    }

    public void refresh(List<BookInfo> list) {
        if (!Utils.isEmptyList(list)) {
            for (int i = 0; i < list.size(); i++) {
                BookInfo item = list.get(i);
                View view = mSparseArray.get(i);
                if (view == null) {
                    view = LayoutInflater.from(getContext()).inflate(R.layout.item_find_layout, null);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    mContainer.addView(view, params);
                    mSparseArray.put(i, view);
                    if (i < list.size() - 1) {
                        View v = new View(getContext());
                        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, DisplayUtils.dip2px(getContext(), 7));
                        mContainer.addView(v, params1);
                    }
                }
                if (TextUtils.isEmpty(item.title_small_img_2)) {
                    item.title_small_img_2 = item.title_small_img;
                }
                GlideImageLoader.load(item.title_small_img, (ImageView) view.findViewById(R.id.cover1));
                GlideImageLoader.load(item.title_small_img_2, (ImageView) view.findViewById(R.id.cover2));
                TextView tv1 = (TextView) view.findViewById(R.id.book_title);
                TextView tv2 = (TextView) view.findViewById(R.id.book_desc);
                tv1.setText(item.title);
                tv2.setText(item.title_desc);
                view.setOnClickListener(v -> {
                    Map<String, String> map = new HashMap<>();
                    map.put("title", item.title);
                    map.put("id", item.id);
                    ARouterUtils.toActivity(ARouterConfig.BOOK_MENU_DETAIL, map);
                });

            }
        }
    }
}
